//////////////////////////////////////////////////////////////////////////////////
//
//	Ejaculate
//		- Version 0.3
//		- 12.09.2006
//		- port: diamond-optic
//		- original: KRoT@L
//
//////////////////////////////////////////////////////////////////////////////////
//
// Information:
//
// 	- Bind a key to "ejaculate" to use
//	- Can be set to admins only
//
// Credits:
//
//   - Original plugin by KRoT@L
//   - Ported to AMXX by diamond-optic
//   - Smiley for idea/request for messages
//
// CVARs & Commands: 
//
//	amx_ejaculate "1"		//Turn ON(1) or OFF(2)
//	amx_ejaculate_admin "0"		//Admins Only - ON(1) or OFF(0)
//	amx_ejaculate_number "1"	//how many times non-admins can use per life (0 = no limit)
//
//	amx_ejaculate_msgmode "0"	//Message Modes:
//						//0 = OFF (no messages)
//						//1 = Chat message
//						//2 = Center message
//						//3 = Hud Message
//
//	amx_ejaculate_msg "$name is ejaculating!"	//Message to be printed (use $name for clients name)
//
// Extra:
//
//	Change the "#define EJAC_ADMIN ADMIN_RESERVATION" line
//	to change the level used for admins
//
//      Put the "ejac.wav" in /sound/misc/  
//
// Changelog:
//
//	 - 06.07.2006 Version 0.1
//		Initial Port
//
//	 - 06.10.2006 Version 0.1b
//		Fixed registering the ResetHud event twice
//
//	 - 06.21.2006 Version 0.1c
//		Added cvar for how many times per life normal clients can use it
//		Admins can always use it as often as they want
//
//	 - 08.09.2006 Version 0.2
//		Fixed a typo in the CVAR & Commands section
//		Changed connect function to putinserver
//		Removed & changed some code
//
//	 - 12.09.2006 Version 0.3
//		Made it so if non-admin number is set to 0 there is no limit
//		Added is_user_alive check in make_ejaculate function
//		Removed some unused commented code
//		Uncommented client_disconnect function
//		Removed client_putinserver function
//		Added screen shake
//		Added losing stamina
//		A few other minor adjustments to the effect
//		Added CVARs to allow a message when someone is ejaculating (per req. from smiley)
//
//////////////////////////////////////////////////////////////////////////////////

#include <amxmodx>
#include <fakemeta>

new bool:EjaculateFlag[33]
new bool:aim[33]
new counter[33]
new playerJizzCount[33]

new p_ejaculate, p_admin, p_number, p_mode, p_msg

#define EJAC_ADMIN ADMIN_RESERVATION		//sets admin level

public plugin_init() 
{ 
	register_plugin("Ejaculate","0.3","KRoTaL & diamond-optic")
	register_cvar("amx_ejaculate_stats", "0.3", FCVAR_SERVER|FCVAR_SPONLY)
	
	p_ejaculate = register_cvar("amx_ejaculate","1")
	p_admin = register_cvar("amx_ejaculate_admin","0")
	p_number = register_cvar("amx_ejaculate_number","1")
	p_mode = register_cvar("amx_ejaculate_msgmode","0")
	p_msg = register_cvar("amx_ejaculate_msg","$name is ejaculating!")
	
	register_clcmd("ejaculate","ejaculate_on_player",0,"- Ejaculate All Over...")
	
	register_event("DeathMsg","death_event","a") 
	register_event("ResetHUD", "reset_ejaculate", "be")
}

public plugin_precache()
{ 
	precache_sound("misc/ejac.wav")
}

public client_disconnect(id)
{
	if(get_pcvar_num(p_ejaculate) != 1 || is_user_bot(id))
		return PLUGIN_CONTINUE

	if(task_exists(4210+id))
		remove_task(4210+id)

	EjaculateFlag[id]=false
	playerJizzCount[id]=0
	
	return PLUGIN_CONTINUE
}

public ejaculate_on_player(id) 
{
	if(!get_pcvar_num(p_ejaculate) || !is_user_connected(id) || is_user_bot(id) || !is_user_alive(id) || ((playerJizzCount[id] > get_pcvar_num(p_number) && get_pcvar_num(p_number) > 0) && !(get_user_flags(id) & EJAC_ADMIN)) || (get_pcvar_num(p_admin) == 1 && !(get_user_flags(id) & EJAC_ADMIN)) || EjaculateFlag[id])
		return PLUGIN_HANDLED
	
	new ids[1]
	
	ids[0]=id
	EjaculateFlag[id]=true
	aim[id]=false
	counter[id]=0
	
	emit_sound(id, CHAN_VOICE, "misc/ejac.wav", 1.0, ATTN_NORM, 0, PITCH_NORM) 
	set_task(1.5,"make_ejaculate",4210+id,ids,1,"a",7)
	
	playerJizzCount[id]++
	
	new msgMode = get_pcvar_num(p_mode)
	
	if(msgMode == 0)
		return PLUGIN_HANDLED
		
	else 
		{
		new msg[128]
		get_pcvar_string(p_msg,msg,127)
		
		new name[32]
		get_user_name(id,name,31)
		replace(msg,127,"$name",name)
			
		if(msgMode == 1)
			client_print(0,print_chat,"%s",msg)
			
		else if(msgMode == 2)
			client_print(0,print_center,"%s",msg)			
			
		else if(msgMode == 3)
			{
			set_hudmessage(255,255,255, 0.02, 0.73, 0, 6.0, 8.0, 0.1, 0.2, 4) 
			show_hudmessage(0,"%s",msg)
			}
		}

	return PLUGIN_HANDLED
}

public sqrt(num) 
{ 
	new div = num 
	new result = 1 
	while (div > result) { 
		div = (div + result) / 2 
		result = num / div 
	} 
	return div 
} 

public make_ejaculate(ids[]) 
{ 
	if(get_pcvar_num(p_ejaculate) != 1 || !is_user_connected(ids[0]) || is_user_bot(ids[0]) || !is_user_alive(ids[0]))
		return PLUGIN_HANDLED
		
	new id=ids[0]
	new vec[3] 
	new aimvec[3] 
	new velocityvec[3] 
	new length 
	get_user_origin(id,vec) 
	get_user_origin(id,aimvec,3) 
	new distance = get_distance(vec,aimvec) 
	new speed = floatround(distance*random_float(1.6,2.2)) //1.9

	velocityvec[0]=aimvec[0]-vec[0] 
	velocityvec[1]=aimvec[1]-vec[1] 
	velocityvec[2]=aimvec[2]-vec[2] 

	length=sqrt(velocityvec[0]*velocityvec[0]+velocityvec[1]*velocityvec[1]+velocityvec[2]*velocityvec[2]) 

	velocityvec[0]=velocityvec[0]*speed/length 
	velocityvec[1]=velocityvec[1]*speed/length 
	velocityvec[2]=velocityvec[2]*speed/length 

	message_begin(MSG_BROADCAST,SVC_TEMPENTITY)
	write_byte(101)
	write_coord(vec[0])
	write_coord(vec[1])
	write_coord(vec[2])
	write_coord(velocityvec[0]) 
	write_coord(velocityvec[1]) 
	write_coord(velocityvec[2]) 
	write_byte(6) // color
	write_byte(random_num(155,165)) // speed
	message_end()
	
	//shake
	message_begin(MSG_ONE_UNRELIABLE,get_user_msgid("ScreenShake"),{0,0,0},id)
	write_short(1<<12) //ammount 
	write_short(2<<12) //lasts this long 
	write_short(185) //frequency
	message_end()	

	//lowers stamina
	new Float:stamina
	pev(id,pev_fuser4,stamina)
	set_pev(id, pev_fuser4, stamina - 50)
	
	counter[id]++
	
	if(counter[id]==7)
		EjaculateFlag[id]=false

	return PLUGIN_CONTINUE
} 

public death_event() 
{ 
   	new victim = read_data(2)

	if(EjaculateFlag[victim] && is_user_connected(victim) && !is_user_bot(victim)) 
		reset_ejaculate(victim)
}

public reset_ejaculate(id) 
{
	if(get_pcvar_num(p_ejaculate) != 1 || is_user_bot(id) || !is_user_connected(id) || EjaculateFlag[id] == false)
		return PLUGIN_CONTINUE

	if(task_exists(4210+id))
		remove_task(4210+id)
	emit_sound(id,CHAN_VOICE,"misc/ejac.wav", 0.0, ATTN_NORM, SND_STOP, PITCH_NORM) 
	EjaculateFlag[id]=false
	playerJizzCount[id]=0

	return PLUGIN_CONTINUE 
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1033\\ f0\\ fs16 \n\\ par }
*/
